package com.sgcc.pms.getway.utils;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.web.server.WebFilterExchange;

import com.alibaba.fastjson.JSON;

import io.netty.util.CharsetUtil;
import reactor.core.publisher.Mono;

public class WriteResponse {

	public static<T> Mono<Void> write(WebFilterExchange webFilterExchange, T data) {
		ServerHttpResponse response = webFilterExchange.getExchange().getResponse();
		response.setStatusCode(HttpStatus.OK);
		response.getHeaders().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
		String body = JSON.toJSONString(data);
		DataBuffer buffer = response.bufferFactory().wrap(body.getBytes(CharsetUtil.UTF_8));
		return response.writeWith(Mono.just(buffer));
	}
	
}
